package com.hxalex.simulationhandle.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.hxalex.simulationhandle.R;
import com.hxalex.simulationhandle.ui.home.model.TaskBean;
import com.hxalex.simulationhandle.ui.home.presenter.TaskPersenter;

import org.greenrobot.eventbus.EventBus;

/**
 * 弹窗
 */

public class TaskPopupWindow extends PopupWindow implements View.OnClickListener{
    public final static String EVENT_SHOW_WINDOW="EVENT_SHOW_WINDOW";
    public final static String EVENT_DISSMISS_WINDOW="EVENT_DISSMISS_WINDOW";
    private Context mContext;
    private TaskBean taskBean;
   private  onDeleteListener onDeleteListener;



    public TaskPopupWindow(Context context){
        this.mContext = context;
        EventBus.getDefault().register(mContext);
      
        init();
    }

    public void setTaskBean(TaskBean taskBean) {
        this.taskBean = taskBean;
    }
   
    public void init(){
        taskPersenter=new TaskPersenter(mContext);
        setAnimationStyle(R.style.popup_show_Animation);
        // 设置弹出窗体的宽和高
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置弹出窗体可点击
        setFocusable(true);
        setBackgroundDrawable(new ColorDrawable(0x00000000));
        //解决华为手机window被navigationBar挡住
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        View rootView = View.inflate(mContext, R.layout.layout_popup_task, null);
        rootView.findViewById(R.id.btn_pause).setOnClickListener(this);
        rootView.findViewById(R.id.btn_stop).setOnClickListener(this);
        rootView.findViewById(R.id.btn_delete).setOnClickListener(this);
        setContentView(rootView);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.btn_pause:
              
                dismiss();
                break;
            case R.id.btn_stop:
               
                dismiss();
                break;
            case R.id.btn_delete:
                delete();
                dismiss();
                break;

        }
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
        EventBus.getDefault().post(EVENT_SHOW_WINDOW);   
    }

    @Override
    public void dismiss() {
        super.dismiss();
        EventBus.getDefault().post(EVENT_DISSMISS_WINDOW);
        EventBus.getDefault().unregister(mContext);
    }

    TaskPersenter taskPersenter;
    private void delete(){
     taskPersenter.deleteTask(taskBean);
     onDeleteListener.onDelete();
    }

    public void setOnDeleteListener(TaskPopupWindow.onDeleteListener onDeleteListener) {
        this.onDeleteListener = onDeleteListener;
    }
    
   public interface  onDeleteListener
    {
        void onDelete();
        
    }
}
